/*
 * Flashlib - QB9 common Flash library
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.qb9.flashlib.easing
{
	import com.qb9.flashlib.tasks.ITask;
	
	/**
	 * Animates one or more attributes of an objects with constant speed.
	 * If more than one attribute is animated, the module of all differences is used.
	 * 
	 * The speed of change is measured on: units/second 
	 */
	
	public class SpeedBasedTween extends Tween
	{
		protected var speed:Number;
		
		public function SpeedBasedTween(obj:Object, speedOfChange:Number, dest:Object, options:Object = null)
		{
			super(obj, 1e5, dest, options);
			
			speed = speedOfChange;
		}
		
		/** @inheritDoc */
		public override function start():void
		{
			super.start();
			
			var sum:Number = 0;
			
			for (var attr:String in dest)
			{
				var diff:Number = dest[attr] - src[attr];
				sum += diff * diff;
			}
			
			duration = Math.round(Math.sqrt(sum) / speed * 1000);
		}
		
		/** @inheritDoc */
		public override function clone():ITask
		{
			return new SpeedBasedTween(obj, speed, dest, options);
		}

	}
}